// pages/mainOperation/handleProgress/handleProgress.js
//获取应用实例
const app = getApp()
let utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isComplete: 0, //是否完结
    PageIndex: 1, //页码
    PageSize: 10, //页大小
    PageCount: 0, //总数量
    orderList: [], //订单列表
    noData: false,
    noDataText:"暂无订单",
    noDataHeight: '330'//暂无数据的高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    this.getOrderList();
  },

  /**
   *  tab菜单切换
   */
  tabTap: function(e) {
    let self = this;
    let isComplete = e.currentTarget.dataset.index;
    if (isComplete != self.data.isComplete) {
      self.setData({
        isComplete: isComplete,
        PageIndex: 1
      });
      self.getOrderList();
    }
  },
  /**
   *  详情跳转
   */
  goToOrderDetailTap: function(e) {
    let self = this;
    let orderId = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '/pages/index/mainOperation/order/orderDetails/orderDetails?OrderId=' + orderId,
    })
  },
  /**
   *  跳转到支付
   */
  goToPaymentTap: function(e) {  
    let orderId = e.currentTarget.dataset.orderid;
    let orderprice = e.currentTarget.dataset.orderprice;
    wx.navigateToMiniProgram({
      appId: 'wx2cad2d484698f6fe',
      path: 'pages/index/index?orderid=' + orderId + '&orderprice=' + orderprice,
      extraData: {
        sourceMini: 'wxb3947f8af58393c9'
      },
      envVersion: 'trial',
      success(res) {
       wx.showModal({
         title: '跳转支付',
         content: JSON.stringify(res),
       })
      }
    })

  },
  /**
   * 查询消息列表
   */
  getOrderList: function() {
    let self = this;
    if (app.userInfo.UserID <= 0) {
      self.setData({
        orderList: [],
        noData: true
      });
      return;
    }
    let url = app.apiBaseUrl + "/RegOrder/GetOrderList";

    app.httpRequest({
      method: "GET",
      url: url,
      showLoading: true,
      data: {
        userId: app.userInfo.UserID,
        isComplete: self.data.isComplete,
        pageIndex: self.data.PageIndex,
        pageSize: self.data.PageSize
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          if (self.data.PageIndex == 1) {
            //根据数据动态显示是否暂无数据
            if (rdata.data.length > 0) {
              //计算总页数
              let recordCount = parseInt(rdata.message);
              if (recordCount > 0) {
                self.data.PageCount = utils.evalPageCount(recordCount, self.data.PageSize);
              }
              self.setData({
                orderList: rdata.data,
                noData: false
              });
            } else {
              self.setData({
                orderList: [],
                noData: true
              });
            }
          } else {
            self.data.orderList.push(...rdata.data);
            self.setData({
              orderList: self.data.orderList
            });
          }
        } else {
          wx.showToast({
            title: rdata.message,
            icon: 'none'
          });
        }
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      PageIndex: 1
    })
    this.getOrderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      PageIndex: this.data.PageIndex + 1
    })
    this.getOrderList();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})